﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Web.BML.VersionData;
using Web.Models.VersionData;
using Web.ViewModel.VersionData;

namespace Web.Service
{
    public  class VersionService : BaseService
    {
        private VersionOp versionOp = VersionOp.GetInstance();

        private static VersionService instance;

        private VersionService()
        {

        }

        public static VersionService GetInstance()
        {
            if (instance == null)
            {
                instance = new VersionService();
            }
            return instance;
        }

        /// <summary>
        /// 获取所有的版本库
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public List<VersionLibModel> GetAllVersionLib()
        {
            return versionOp.QueryAll().Select(m=>VersionLibModel.ConvertModel(m)).ToList();
        }

        /// <summary>
        /// 获取指定版本库
        /// </summary>
        /// <param name="shortName">简称</param>
        /// <returns></returns>
        public IServiceResult GetVersionLib(Guid id)
        {
            if(id==Guid.Empty)
            {
                return Result(ResultType.ParamIsNull,"您请求的参数错误");
            }
            VersionLibModel model = VersionLibModel.ConvertModel(versionOp.QueryVersionLib(id));
            return Result(ResultType.Success,model);
        }

     


        /// <summary>
        /// 添加版本库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IServiceResult AddVersionLib(VersionLibModel model)
        {
            if(ParamsIsNull(model.ShortName))
            {
                return Result(ResultType.ParamIsNull);
            }

            if (versionOp.AddVersionLib(VersionLibModel.ToEntity(model)))
            {
                return Result(ResultType.Success);
            }else
            {
                return Result(ResultType.Fail);
            }
        }

        /// <summary>
        /// 修改版本库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IServiceResult ModifyVersionLib(VersionLibModel model)
        {
            if (ParamsIsNull(model.ShortName))
            {
                return Result(ResultType.ParamIsNull);
            }
            bool result = false;
            var data = versionOp.QueryVersionLib(model.Id);
            if (data==null)
            {
                model.Id = Guid.NewGuid();
                model.UpdateTime = DateTime.Now;
                result=versionOp.AddVersionLib(VersionLibModel.ToEntity(model));
            }
            else
            {
                data.Description = model.Description;
                data.ProjectName = model.ProjectName;
                data.ShortName = model.ShortName;
                result =versionOp.ModifyVersionLib(data);
            }

            if (result)
            {
                return Result(ResultType.Success);
            }
            else
            {
                return Result(ResultType.Fail);
            }
        }

    }
}